<template>
  <div class="flex">
    <BasicTable @register="registerTable" class="w-1/2" @row-click="handleRowClick">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: '',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: '',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否删除数据？',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DictData class="w-1/2" :searchInfo="searchInfo" />
    <DictTypeModal @register="registerModal" @success="reload()" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import DictData from './DictData.vue';
  import DictTypeModal from './DictTypeModal.vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { typeColumns, typeSearchFormSchema } from './dict.type';
  import { deletedictData, dictDataList } from '/@/api/backs/system/dictManage';

  defineOptions({ name: 'SystemDict' });

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const searchInfo = reactive<Recordable>({});

  const [registerTable, { reload }] = useTable({
    title: '字典分类列表',
    api: dictDataList,
    columns: typeColumns,
    formConfig: {
      labelWidth: 120,
      schemas: typeSearchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  });

  function handleRowClick(record) {
    searchInfo.dictType = record.type;
  }

  function handleCreate() {
    openModal(true, { isUpdate: false });
  }

  function handleEdit(record: Recordable) {
    openModal(true, { record, isUpdate: true });
  }

  async function handleDelete(record: Recordable) {
    await deletedictData({ id: record.id });
    createMessage.success('删除成功！');
    reload();
  }
</script>
